<template>
    <div class="panel-body">
        <div class="panel-header">
            <div class="panel-title">
                {{ meta_name }}
            </div>
            <el-tabs v-model="params.group" type="card" @tab-click="search()">
                <el-tab-pane label="全 部" name="0"></el-tab-pane>
                <el-tab-pane label="关于我们" name="1"></el-tab-pane>
                <el-tab-pane label="常见问题" name="2"></el-tab-pane>
                <el-tab-pane label="积分规则" name="3"></el-tab-pane>
            </el-tabs>
        </div>
        <div class="panel-main">
            <div class="panel-btns">
                <el-button type="success" icon="fa fa-refresh" @click="list()" size="small"> 刷新</el-button>
                <el-button type="primary" icon="fa fa-plus" @click="create()" v-show="access(1601)" size="small"> 新增</el-button>
            </div>
            <div class="panel-search">
                <div class="panel-search-item">
                    <el-input  size="medium" v-model="params.kwd" clearable placeholder="请输入单页标题" class="input-with-select">
                        <el-button type="primary" @click="search()" slot="append" icon="el-icon-search"></el-button>
                    </el-input>
                </div>
            </div>
            <el-table v-loading="loading"
                      element-loading-text="努力加载中..."
                      element-loading-spinner="el-icon-loading"
                      ref="multipleHandle"
                      tooltip-effect="dark"
                      :data="listData.data" border>
                <el-table-column align="center" prop="id" label="编号" width="60"></el-table-column>
                <el-table-column prop="title" :show-overflow-tooltip="true" label="标题" width="350"></el-table-column>
                <el-table-column align="center" prop="group_text" label="分组" width="100"></el-table-column>
                <el-table-column align="center" prop="created_at" label="发表日期" width="160">
                    <template slot-scope="scope">
                        {{moment(scope.row.created_at).format('YYYY-MM-DD HH:mm:ss')}}
                    </template>
                </el-table-column>
                <el-table-column label="操作" >
                    <template slot-scope="scope">
                        <el-button @click="edit(scope.row)" v-show="access(1602)" icon="fa fa-edit" type="primary" size="mini" plain> 编辑</el-button>
                        <el-button @click="destroy(scope.row.id)" v-show="access(1603)" icon="fa fa-trash" type="info" size="mini" plain> 删除</el-button>
                    </template>
                </el-table-column>
            </el-table>
            <div class="pagination"  v-if="!loading && listData && listData.total">
                <div class="pagination-total">共 <em> {{listData.total}} </em> 条记录</div>
                <el-pagination v-if="listData.last_page > 1"
                               background
                               :page-size="listData.per_page"
                               layout="prev, pager, next, jumper"
                               :total="listData.total"
                               prev-text="上一页"
                               next-text="下一页"
                               :current-page="listData.current_page"
                               @current-change="changePage">
                </el-pagination>
            </div>
        </div>
    </div>
</template>
<!---->
<script>
    import {mapGetters} from 'vuex';
    import {PageList,PageDestroy} from '@/utils/request';
    export default {
        computed: {
            ...mapGetters(['meta_name','loading']),
        },
        data() {
            return {
                listData: [],//列表数据
                cardVal: 0,
                params: {page: 1, kwd: '', group: 0},
                cates: []
            }
        },
        activated() {
            this.list();
        },
        methods: {
            //新增
            create(){
                this.$store.dispatch('setDetail', null);
                this.$router.push({name:'PageUpdate'});
            },
            //编辑
            edit(info){
                this.$store.dispatch('setDetail', info);
                this.$router.push({name:'PageUpdate',query:{id:info.id}});
            },
            //列表
            list(){
                this.$store.dispatch('setLoading', true);
                PageList({params: this.params}).then((res)=>{
                    if (res.code) {
                        this.$notify.error({title: 'ERROR', message: res.msg});
                    } else {
                        this.listData = res.data;
                    }
                    this.$store.dispatch('setLoading', false);
                })
            },
            //筛选
            search(){
                this.params.page = 1;
                if(this.cardVal == 3){
                    this.params.status = 0;
                    this.params.good = 2;
                }else{
                    this.params.status = this.cardVal;
                    this.params.good = 0;
                }
                this.list();
            },
            //分页
            changePage(val) {
                this.params.page = val;
                this.list();
            },
            //删除
            destroy(id){
                this.$confirm('确定要删除该单页信息吗？', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                }).then(() => {
                    PageDestroy({id: id}).then((res) => {
                        if (res.code) {
                            this.$notify.error({title: 'ERROR', message: res.msg});
                        } else {
                            this.list();
                        }
                    });
                }).catch(() => {});
            },
        }
    }
</script>
